package cn.com.utt.unms.controller;

import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;

import cn.com.utt.common.util.LocaleMessageSourceUtil;
import cn.com.utt.common.util.Tips;
import cn.com.utt.common.vo.RestResult;
import cn.com.utt.common.vo.TableInput;
import cn.com.utt.common.vo.TableOutput;
import cn.com.utt.common.vo.User;
import cn.com.utt.unms.pojo.Device;
import cn.com.utt.unms.pojo.Location;
import cn.com.utt.unms.pojo.Network;
import cn.com.utt.unms.pojo.Wireless;
import cn.com.utt.unms.service.DeviceService;
import cn.com.utt.unms.service.GroupService;
import cn.com.utt.unms.service.NetworkService;
import cn.com.utt.unms.service.WirelessService;
import cn.com.utt.unms.vo.NetworkInfo;
import cn.com.utt.unms.vo.NetworkInput;

@RestController
@RequestMapping("v1/networks")
public class NetworkController {
	
	@Autowired
    private LocaleMessageSourceUtil messageSourceUtil;
	
	@Autowired
	private NetworkService networkService;
	
	@Autowired
	private DeviceService deviceService;
	
	@Autowired
	private GroupService groupService;
	
	@Autowired
	private WirelessService wirelessService;
	
	@RequestMapping(value = "/save", method = { RequestMethod.POST }, consumes = "application/json")
	public Map<String, Object> save(@RequestBody NetworkInput networkInput,Principal principal) {
		Authentication authentication = (Authentication) principal;
		User user = (User)authentication.getPrincipal();
		RestResult result = new RestResult();
		if(networkInput.getGroupId() == null || networkInput.getNetwork() == null){
			result.setErrorCode(1);
			result.setErrorMsg(messageSourceUtil.getMessage(Tips.InvalidParameter));
			return result.toJsonObject();
		}
		if(!groupService.hasRight(user.getGroupId(),networkInput.getGroupId())){
			result.setErrorMsg(messageSourceUtil.getMessage(Tips.PermissionDenied));
			return result.toJsonObject();
		}
		result = networkService.add(networkInput, user.getGroupId());
		return result.toJsonObject();
	}

	@RequestMapping(value = "/update", method = { RequestMethod.POST }, consumes = "application/json")
	public Map<String, Object> update(@RequestBody NetworkInput networkInput,Principal principal) {
		Authentication authentication = (Authentication) principal;
		User user = (User)authentication.getPrincipal();
		RestResult result = new RestResult();
		
		if(networkInput.getGroupId() == null || networkInput.getNetwork() == null){
			result.setErrorCode(1);
			result.setErrorMsg(messageSourceUtil.getMessage(Tips.InvalidParameter));
			return result.toJsonObject();
		}
		Network network = networkInput.getNetwork();
		if(network == null || network.getId() == 0 || !networkService.hasRight(user.getGroupId(),new ArrayList<Long>(){{network.getId();}})){
			result.setErrorCode(1);
			result.setErrorMsg(messageSourceUtil.getMessage(Tips.PermissionDenied));
			return result.toJsonObject();
		}
		result = networkService.update(networkInput, user.getGroupId());
		return result.toJsonObject();
	}
	
	@RequestMapping(value = "/updateName", method = { RequestMethod.POST }, consumes = "application/json")
	public Map<String, Object> updateName(@RequestBody Network network,Principal principal) {
		Authentication authentication = (Authentication) principal;
		User user = (User)authentication.getPrincipal();
		RestResult result = new RestResult();
		if(network == null || network.getId() == 0 || !networkService.hasRight(user.getGroupId(),new ArrayList<Long>(){{network.getId();}})){
			result.setErrorCode(1);
			result.setErrorMsg(messageSourceUtil.getMessage(Tips.PermissionDenied));
			return result.toJsonObject();
		}
		networkService.updateName(network);
		result.setErrorMsg(messageSourceUtil.getMessage(Tips.OperationSucceed));
		return result.toJsonObject();
	}
	
	@RequestMapping(value = "/delete", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> delete(@RequestParam(value = "networkIds[]")List<Long> networkIds,Principal principal) {
		Authentication authentication = (Authentication) principal;
		User user = (User)authentication.getPrincipal();
		RestResult result = new RestResult();
		if(networkIds == null || networkIds.size() == 0){
			result.setErrorCode(1);
			result.setErrorMsg(messageSourceUtil.getMessage(Tips.InvalidParameter));
			return result.toJsonObject();
		}
		if(networkIds == null || !networkService.hasRight(user.getGroupId(),networkIds)){
			result.setErrorCode(1);
			result.setErrorMsg(messageSourceUtil.getMessage(Tips.PermissionDenied));
			return result.toJsonObject();
		}
		result = networkService.delete(networkIds);
		return result.toJsonObject();
	}
	
	/**
	 * @param page
	 *            页码
	 * @param rows
	 *            每页显示条数
	 * @return 分组列表
	 */
	@RequestMapping(value = "/list", method = { RequestMethod.POST }, consumes = "application/json")
	public Map<String, Object> list(@RequestBody TableInput input,Principal principal) {
		Authentication authentication = (Authentication) principal;
		User user = (User)authentication.getPrincipal();
		RestResult result = new RestResult();
		TableOutput<NetworkInfo> output = networkService.queryList(input,user.getGroupId());
		result.put("start", output.getStart());
		result.put("rows", output.getRows());
		result.put("total", output.getTotal());
		result.put("groups", output.getList());
		if (user.hasRole("ROLE_NETWORK_EDIT")) {
			result.put("hasEditRole", true);
		} else {
			result.put("hasEditRole", false);
		}
		return result.toJsonObject();
	}
	
	@RequestMapping(value = "/load", method = { RequestMethod.POST }, consumes = "application/json")
	public Map<String, Object> load(@RequestBody Network network) {
		RestResult result = new RestResult();
		
		if(network == null){
			result.setErrorCode(1);
			result.setErrorMsg(messageSourceUtil.getMessage(Tips.InvalidParameter));
			return result.toJsonObject();
		}
		if(network.getLocationId() != 0){
			Location location = networkService.queryLocation(network.getLocationId());
			result.put("location", location);
		}else{
			result.put("location", null);
		}
		List<Device> devices = deviceService.queryDevicesByNetworkIds(new ArrayList<Long>(){{add(network.getId());}});
		SimplePropertyPreFilter deviceFilter = new SimplePropertyPreFilter(
				Device.class, "id", "name");
		result.put("devices", JSON.parse(JSON.toJSONString(devices, deviceFilter)));
		List<Wireless> wirelesses = wirelessService.queryWirelessesByNetwork(network.getId());
		SimplePropertyPreFilter wirelessFilter = new SimplePropertyPreFilter(
				Wireless.class, "id", "name");
		result.put("wirelesses", JSON.parse(JSON.toJSONString(wirelesses, wirelessFilter)));
		result.setErrorCode(0);
		return result.toJsonObject();
	}
	
}
